package com.wtp;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebActivity extends Activity {

	private WebView webView;
	private CustomDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_web);
		initview();
	}

	private void initview() {
		dialog = DialogUtil.loading(this);
		webView = (WebView) findViewById(R.id.webview);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setDefaultTextEncodingName("utf-8");
		webView.setBackgroundColor(Color.argb(0, 0, 0, 0));
		webView.addJavascriptInterface(new JavaScriptObject(), "JS");
		webView.loadUrl(getIntent().getStringExtra("url"));
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				dialog.CloseDialog();
				super.onPageFinished(view, url);
			}
		});
	}

	private class JavaScriptObject {
		@JavascriptInterface
		public void FinishWebView() {
			finish();
		}
		@JavascriptInterface
		public void LoadUrl(String url){
			webView.loadUrl(url);
		}
		@JavascriptInterface
		public void WebViewBack(){
			webView.canGoBack();
		}
		@JavascriptInterface
		public void SetInfo(String info){
			finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.web, menu);
		return true;
	}

}
